from ttzScrapyProject.constants.UrlConstant import LOTTERY_ISSUE_URL, LOTTERY_HISTORY_URL
from ttzScrapyProject.dao.BaseDao import MysqlDao
from ttzScrapyProject.utils import HttpUtils


class ModelDao(MysqlDao):
    def __init__(self):
        super().__init__()
        self.cursor = self.getCursor()
        self.db = self.getDB()
        self.tableName = 'model'

    def buy(self, loginUserVo):
        lastTransactionUrl = f"{LOTTERY_HISTORY_URL}{loginUserVo.usertoken}&g28=luck&Type=issue&PageNumber=1"
        historyData = HttpUtils.get(operation="获取历史下注数据", url=lastTransactionUrl)
        detailData = historyData["Data"]
        dataList = detailData["List"]["Rows"][0:1]

        for index, item in enumerate(dataList):
            #  期号
            detailDataId = item["Id"]
            if loginUserVo.lotteryIssue == detailDataId:
                print(f"检测到当前期已下注不可重复下注{loginUserVo.lotteryIssue}")
                return loginUserVo
        transactionUrl = f"{LOTTERY_ISSUE_URL}{loginUserVo.usertoken}&g28=luck&arr={loginUserVo.arr}&luckId={loginUserVo.lotteryIssue}"
        data = HttpUtils.get("下注买入", transactionUrl)
        if data == "":
            return loginUserVo
        
        if data["Msg"] == "参与成功":
            print("下注成功")
        print("下注完成,检查")
        return loginUserVo


# if __name__ == '__main__':
#     print("1" =="1")
